package ar.edu.utn.frsf.cidisi.ginet.generation.upcolbpip2cginet.managers;

import org.eclipse.emf.common.util.EList;
import org.pnml.tools.epnk.pntypes.hlpng.pntd.hlpngdefinition.Declaration;

import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Page;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Place;
import ar.edu.utn.frsf.cidisi.ginet.formalization.FormalAssociation;
import ar.edu.utn.frsf.cidisi.ginet.formalization.FormalCBPLanguage;
import ar.edu.utn.frsf.cidisi.ginet.generation.upcolbpip2cginet.elementstemplate.ChoreographyStructure;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.And;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.BusinessMessage;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Cancel;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Exception;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.If;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Loop;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.MultipleInstance;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Or;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Termination;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Xor;

public class PageGiNetCreator {
	
	public static Page createAbstractPage(InteractionProtocolElement element, int pageCount, Place placeIn, Place placeOut, EList<Declaration> ginetDeclarations, ar.edu.utn.frsf.cidisi.ginet.abstractginet.Place termination, EList<BusinessMessage> messages) {
		
		String elementname;
		if(element != null){
			elementname = getInteractionProtocolElementName(element);
		}else{
			elementname = "BUSINESSMESSAGE";
		}		
		
		ChoreographyStructure choreagrapy = getTemplateByElement(elementname, pageCount, new EMFModelLoad().load());
		choreagrapy.createPageStructure(element, placeIn, placeOut, ginetDeclarations, termination, messages);
		
		return choreagrapy.getPage();
	}
	
	
	/**
	 * @param name
	 * @return
	 *
	 * @author Neri
	 */
	public static ChoreographyStructure getTemplateByElement(String name, int count, FormalCBPLanguage language) {
		
		EList<FormalAssociation> formalAssociations = language.getFormalizedElements();
		
		for (FormalAssociation formalAssociation : formalAssociations) {
			if(formalAssociation.getMetamodelElement().getElement().get(0).getName().toUpperCase().equals(name)){
				String className = formalAssociation.getFormalElement().getElement().getId();
				Class<?> classTemplate = null;
				try {
					classTemplate = Class.forName("ar.edu.utn.frsf.cidisi.ginet.generation.upcolbpip2cginet.elementstemplate." + className);
					return (ChoreographyStructure)classTemplate.getConstructor(Integer.class).newInstance(new Integer(count));
				} catch (java.lang.Exception e) {
					return null;
				}
			}
		}
		return null;
	}

	public static String getInteractionProtocolElementName(
			InteractionProtocolElement ipe) {

		if (ipe instanceof And) {
			return "AND";
		}
		if (ipe instanceof Or) {
			return "OR";
		}
		if (ipe instanceof Xor) {
			return "XOR";
		}
		if (ipe instanceof If) {
			return "IF";
		}
		if (ipe instanceof Loop) {
			return "LOOP";
		}
		if (ipe instanceof Exception) {
			return "EXCEPTION";
		}
		if (ipe instanceof Cancel) {
			return "CANCEL";
		}
		if (ipe instanceof Termination) {
			return "Termination";
		}
		if (ipe instanceof BusinessMessage) {
			return "BUSINESSMESSAGE";
		}
		if (ipe instanceof MultipleInstance) {
			return "MULTIPLEINSTANCE";
		}

		return null;
	}

}
